/*
 * To change this template, choose Tools | Templates
 * and open the template i
 */
package proyectotsm;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;
import java.io.File;
import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import org.jvnet.substance.SubstanceLookAndFeel;

/**
 * Clase GUI.java
 * 
 * @author Srgank
 * @author JuanCarlos
 * @author Gabriel
 */
public class GUI extends javax.swing.JFrame {

    // Atributo
    private Collection<Imagen> imagenes;

    /**
     * Constructor
     */
    public GUI() {

        lector = new Lector();
        JFrame.setDefaultLookAndFeelDecorated(true);
        JDialog.setDefaultLookAndFeelDecorated(true);
        SubstanceLookAndFeel.setSkin("org.jvnet.substance.skin.RavenGraphiteGlassSkin");

        // Seteamos los atributos
        ab = new About();
        controles = new Controles();
        initComponents();
        this.createBufferStrategy(2);
        bf = getBufferStrategy();
        runing = false;
        par = new ParametrosCompresion();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Archivos = new javax.swing.JFileChooser();
        PanelPrincipal = new javax.swing.JPanel();
        Visor = new javax.swing.JLabel();
        Play = new javax.swing.JToggleButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        output = new javax.swing.JTextArea();
        progress = new javax.swing.JSlider();
        tiempo = new javax.swing.JLabel();
        framespersecond = new javax.swing.JLabel();
        videoInfo = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        Pause = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        Controles = new javax.swing.JMenuItem();
        Abrir = new javax.swing.JMenuItem();
        Guardar = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();
        parametrosCompresion = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();

        Archivos.setVisible(false);
        Archivos.setFileFilter(new javax.swing.filechooser.FileFilter() {public boolean accept(File f) {
            return f.isDirectory() || f.getName().toLowerCase().endsWith(".zip") || f.getName().toLowerCase().endsWith(".dof");
        }

        public String getDescription() {
            return ".zip or .DOF files";
        }});
        Archivos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ArchivosActionPerformed(evt);
            }
        });

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Reproductor JAVA, by DJG Software Labs");
        setBounds(new java.awt.Rectangle(0, 0, 800, 600));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        PanelPrincipal.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        PanelPrincipal.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        Visor.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        Play.setText("Play");
        Play.setEnabled(false);
        Play.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                PlayMousePressed(evt);
            }
        });
        Play.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                PlayStateChanged(evt);
            }
        });
        Play.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PlayActionPerformed(evt);
            }
        });

        output.setColumns(20);
        output.setRows(5);
        output.setEnabled(false);
        jScrollPane1.setViewportView(output);

        progress.setEnabled(false);

        tiempo.setText("00:00");

        videoInfo.setFont(new java.awt.Font("Tahoma", 1, 14));
        videoInfo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        videoInfo.setText("CARGUE UN VIDEO Y PULSE PLAY");

        jLabel2.setText("f.p.s:");

        Pause.setText("Pause");
        Pause.setEnabled(false);
        Pause.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                PauseMousePressed(evt);
            }
        });

        javax.swing.GroupLayout PanelPrincipalLayout = new javax.swing.GroupLayout(PanelPrincipal);
        PanelPrincipal.setLayout(PanelPrincipalLayout);
        PanelPrincipalLayout.setHorizontalGroup(
            PanelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PanelPrincipalLayout.createSequentialGroup()
                .addGroup(PanelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(PanelPrincipalLayout.createSequentialGroup()
                        .addGroup(PanelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(PanelPrincipalLayout.createSequentialGroup()
                                .addGap(246, 246, 246)
                                .addComponent(tiempo))
                            .addGroup(PanelPrincipalLayout.createSequentialGroup()
                                .addGap(104, 104, 104)
                                .addComponent(Visor, javax.swing.GroupLayout.PREFERRED_SIZE, 321, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel2))
                            .addGroup(PanelPrincipalLayout.createSequentialGroup()
                                .addGap(163, 163, 163)
                                .addComponent(progress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(PanelPrincipalLayout.createSequentialGroup()
                                .addGap(151, 151, 151)
                                .addGroup(PanelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(PanelPrincipalLayout.createSequentialGroup()
                                        .addComponent(Play)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(Pause))
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(2, 2, 2)
                        .addComponent(framespersecond))
                    .addGroup(PanelPrincipalLayout.createSequentialGroup()
                        .addGap(118, 118, 118)
                        .addComponent(videoInfo, javax.swing.GroupLayout.PREFERRED_SIZE, 276, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(52, Short.MAX_VALUE))
        );
        PanelPrincipalLayout.setVerticalGroup(
            PanelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PanelPrincipalLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tiempo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(progress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(PanelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(PanelPrincipalLayout.createSequentialGroup()
                        .addComponent(Visor, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addGroup(PanelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(Play)
                            .addComponent(Pause))
                        .addGap(18, 18, 18))
                    .addGroup(PanelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(framespersecond)
                        .addComponent(jLabel2)))
                .addGap(18, 18, 18)
                .addComponent(videoInfo)
                .addContainerGap())
        );

        jMenu1.setText("Archivo");
        jMenu1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMenu1MouseClicked(evt);
            }
        });

        Controles.setText("Controles");
        Controles.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                ControlesMousePressed(evt);
            }
        });
        jMenu1.add(Controles);

        Abrir.setText("Abrir");
        Abrir.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                AbrirMousePressed(evt);
            }
        });
        jMenu1.add(Abrir);

        Guardar.setText("Guardar");
        Guardar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                GuardarMousePressed(evt);
            }
        });
        jMenu1.add(Guardar);

        jMenuItem1.setText("Guardar Comprimido");
        jMenuItem1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jMenuItem1MousePressed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        parametrosCompresion.setText("Parametros compresion");
        parametrosCompresion.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                parametrosCompresionMousePressed(evt);
            }
        });
        jMenu1.add(parametrosCompresion);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Ayuda");

        jMenuItem2.setText("About");
        jMenuItem2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jMenuItem2MousePressed(evt);
            }
        });
        jMenu2.add(jMenuItem2);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(PanelPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(46, Short.MAX_VALUE)
                .addComponent(PanelPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenu1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenu1MouseClicked
    }//GEN-LAST:event_jMenu1MouseClicked

    private void AbrirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AbrirActionPerformed
    }//GEN-LAST:event_AbrirActionPerformed

    private void ArchivosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ArchivosActionPerformed
    }//GEN-LAST:event_ArchivosActionPerformed

    private void PlayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PlayActionPerformed
}//GEN-LAST:event_PlayActionPerformed

    private void PlayStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_PlayStateChanged
}//GEN-LAST:event_PlayStateChanged

    private void PlayMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_PlayMousePressed
        /* Si las imagenes no estan cargadas podemos darle al play */
        if (cargado == true) {
            if (playing == true && pause != true) {
                playing = false;
            } else {
                playing = true;
                pause = false;
                videoInfo.setText("[ REPRODUCIENDO VIDEO ]");
                progreso = 0;
                progress.setValue(0);
                tiempo.setText("00:00:00");
                tiempo1 = System.currentTimeMillis();
                runing = true;
            }
        } else {
            videoInfo.setText("NO HAY NINGUN VIDEO CARGADO !!!");
        }
}//GEN-LAST:event_PlayMousePressed

    private void PauseMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_PauseMousePressed
        pause = !pause;
}//GEN-LAST:event_PauseMousePressed
    public boolean isruning() {
        return runing;
    }
    private void jMenuItem1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenuItem1MousePressed
        Archivos.setVisible(true);
        int returnValue = Archivos.showSaveDialog(this);
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            File file = Archivos.getSelectedFile();

            // lector.saveaszip(file.toString());
            lector.setAnchoteselas(par.getAnchoteselas());
            lector.setAltoteselas(par.getAltoteselas());
            lector.setFactorCompresion(par.getParametroCompresion());
            lector.setRecur(par.getRecur());
            lector.setRango(par.getRangoImagenes());
            lector.saveNewCompress(file.toString(), true);
        }
    }//GEN-LAST:event_jMenuItem1MousePressed

    private void jMenuItem2MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenuItem2MousePressed
        ab.setVisible(true);
    }//GEN-LAST:event_jMenuItem2MousePressed

    private void ControlesMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ControlesMousePressed
        controles.setVisible(true);
    }//GEN-LAST:event_ControlesMousePressed

    private void GuardarMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_GuardarMousePressed
        Archivos.setVisible(true);
        int returnValue = Archivos.showSaveDialog(this);
        if (returnValue == JFileChooser.APPROVE_OPTION) {

            File file = Archivos.getSelectedFile();
            lector.saveNewCompress(file.toString(), false);
        }
    }//GEN-LAST:event_GuardarMousePressed

    private void AbrirMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_AbrirMousePressed
        Archivos.setVisible(true);
        int returnValue = Archivos.showOpenDialog(this);

        if (returnValue == JFileChooser.APPROVE_OPTION) {
            File file = Archivos.getSelectedFile();
            lector.setArchivo(file.getPath());
            lector.cargarImagenes(output);
            cargado = true;
            videoInfo.setText("[ VIDEO CARGADO CORRECTAMENTE ]");
            imagenes = lector.getImagenes();
            i = imagenes.iterator();
            Archivos.setVisible(false);
            step = imagenes.size() / 100;
            progress.setValue(0);
            Play.setEnabled(true);
            Pause.setEnabled(true);
        }
    }//GEN-LAST:event_AbrirMousePressed

    private void parametrosCompresionMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_parametrosCompresionMousePressed

        par.setVisible(true);
    }//GEN-LAST:event_parametrosCompresionMousePressed

    @Override
    public void paintComponents(Graphics g) {
        Graphics2D g2 = null;
        g2 = (Graphics2D) bf.getDrawGraphics();

        if (playing == true && pause != true) {
            super.paintComponents(g2);

            if (i.hasNext()) {
                Imagen img = i.next();
                if (controles.istinv()) {
                    Visor.setIcon(new ImageIcon(img.getInvertedImage()));
                } else if (controles.isTher()) {
                    Visor.setIcon(new ImageIcon(img.getTherImage(controles.getValor())));
                } else if (controles.issepia()) {
                    Visor.setIcon(new ImageIcon(img.getSepiaImage()));
                } else {
                    Visor.setIcon(new ImageIcon(img.getImagen()));
                }
                progress.setValue((int) progreso);
                progreso += step;
                tiempo2 = System.currentTimeMillis() - tiempo1;
                tiempo.setText(String.format("%d:%d",
                        TimeUnit.MILLISECONDS.toMinutes(tiempo2),
                        TimeUnit.MILLISECONDS.toSeconds(tiempo2)
                        - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(tiempo2))));
                System.out.println(img.getNombre());
            } else {
                i = imagenes.iterator();
                playing = false;
                videoInfo.setText("[ REPRODUCCION TERMINADA ]");
                runing = false;
            }
            if (!pause) {
                Visor.paintComponents(g2);
            }
            g2.dispose();
            bf.show();
        }
    }

    public void setFps(float fps) {
        this.fps = fps;
        framespersecond.setText(String.valueOf(this.fps));
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                GUI gui = new GUI();
                gui.setVisible(true);
                manager = new Manager(gui);

                th = new Thread(manager);
                started = false;
                th.start();
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem Abrir;
    private javax.swing.JFileChooser Archivos;
    private javax.swing.JMenuItem Controles;
    private javax.swing.JMenuItem Guardar;
    private javax.swing.JPanel PanelPrincipal;
    private javax.swing.JButton Pause;
    private javax.swing.JToggleButton Play;
    private javax.swing.JLabel Visor;
    private javax.swing.JLabel framespersecond;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea output;
    private javax.swing.JMenuItem parametrosCompresion;
    private javax.swing.JSlider progress;
    private javax.swing.JLabel tiempo;
    private javax.swing.JLabel videoInfo;
    // End of variables declaration//GEN-END:variables
    private boolean playing;
    private Iterator<Imagen> i;
    private final BufferStrategy bf;
    private Lector lector;
    private boolean done;
    private double step;
    private double progreso;
    private long tiempo1;
    private long tiempo2;
    private boolean cargado = false;
    private float fps;
    private boolean pause;
    private About ab;
    private boolean runing;
    private Controles controles;
    static Runnable manager;
    static Thread th;
    static private boolean started;
    ParametrosCompresion par;
}
